<?php

session_start();
include './source/class/class_core.php';
if (!$_SESSION['name'])
	header('Location: index.php');

$name = $_SESSION['name'];
	
if(isset($_GET['action']) & $_GET['action'] == 'lock' & $name == 'MM'){
	
	// 根据文件修改时间确保只执行一次
	if (date('Y-m-d') != date("Y-m-d", filectime('info.txt'))){
	
		// 锁单之后产生一免单用户
		$date_time = strtotime(date('Y-m-d'));
		$source = DB::query("SELECT * FROM `om_order` WHERE order_index = '$date_time'");
		
		//echo '<pre>';
		while ($row = DB::fetch($source)){
			$user_data = unserialize($row['order_info']);
			$order_index[$row['name']] = array_keys($user_data['num']);
		}
		
		// 所有十块钱的菜的下标
		$caidan = array();
		include 'caidan.php';
		foreach ($caidan as $k => $v){
			if ($v['price'] >= 10){
				$more_ten[] = $k;
			}
		}
		
		
		$nafan = array_rand($order_index, 1);
		
		foreach ($order_index as $key => $val){
			foreach ($val as $k => $v){
				if (!in_array($v, $more_ten)){
					unset($order_index[$key][$k]);
					if (empty($order_index[$key])){
						unset($order_index[$key]);
					}
				}
			}
		}
		
		
		// 免单的人
		$ren = array_rand($order_index, 1);
		$fan = $order_index[$ren][array_rand($order_index[$ren], 1)];
		//echo $ren . $fan . '<br>';
		
		
		$info = '今日免单【' . $ren . '】=【' . $caidan[$fan]['name'] . '】  <br>【' . $nafan . '】同学今天去拿饭';
		file_put_contents('info.txt', $info);
		
		//print_r($order_index);
	}
	
	
	
	$value = $_GET['value'];
	file_put_contents('lock.php', $value);
	header("Content-type: text/html; charset=utf-8");
	echo "<script>window.location.href='index.php';</script>";
}
